:: 文件： bat模板
:: 版权： Copyright 谢振斌  All Rights Reserved.  
:: 作者： 谢振斌  
:: 时间： 2021-3-30 

:: 用途：

:: 在bat里有隐藏窗口的命令，很简单，只需要在代码头部加@echo off代码就可以了。 
@echo off
:: 调用设置当前前端口为utf-8 的编码格式函数
call:setDosCodingFormatUTF8

:: echo.开始调用函数 >> debug.log  | TYPE debug.log

:: 获取当前脚本名字
::    获取当前批处理文件的全路径
set filePath=%~f0

for %%a in ("%filepath%") do (
    set str=%%~pda
    :: 文件名赋值
    set var=%%~nxa
)
::echo 路径：%str%
::echo 文件名：%var%
::pause

:: 日志文件名
::set logFileName=Template
set logFileName=%var%

rem set flag="Test"
set flag="Run"
:: echo flag=%flag% 
if %flag% == "Run" (
    echo. 执行流程控制函数
    call:processControlMain >> %logFileName%.log
    ) ELSE (
    echo. 执行测试函数 
    ::call:myDosFunc  >> debug.log
    rem echo. [debug] 测试 - setSystemEnvValue==
    rem call:setSystemEnvValue  >> debug.log

    echo. [debug] 测试 - setUserEnvValue==
    set namesssx=xxxxx
    set pathvaluesss=xxxxx
    echo. [debug] 测试 - name==
    echo. %namesssx%
    echo. [debug] 测试 - pathvalue==
    echo. %pathvaluesss%
    call:setUserEnvValue  %namesssx%  %pathvaluesss% >> debug.log
    rem call:setUserEnvValue  %namesssx%  %pathvaluesss% >> debug.log | TYPE debug.log
    rem echo. [debug] 测试 - processControlMain==
    ::call:processControlMain >> debug.log
    :: call:processControlMain >> debug.log  | TYPE debug.log
    :: 注：加重定向输出>> debug.log  | TYPE debug.log 有时会报错
    :: 窗口暂停
    pause
    )

::--------------------------------------------------------
::-- 函数部分开始
::--------------------------------------------------------
:: 函数:
:myDosFunc    - here starts my function identified by it`s label
    echo. [INFO] here the myDosFunc function is executing a group of commands
    echo. [INFO] it could do a lot of things
goto:eof

:: 函数: 设置当前前端口为utf-8 的编码格式
:setDosCodingFormatUTF8    - 设置当前前端口为utf-8 的编码格式
    :: 设置当前前端口为utf-8 的编码格式
    chcp 65001
goto:eof

:: 函数: 设置文件链接
:makeFileLinkFunc    - makeFileLinkFunc
    echo. [INFO] makeFileLinFunc === Start ===
    echo. [INFO] makeFileLinFunc === 1 2 ===
    echo. [Debug] %1
    echo. [Debug] %2

    echo. [INFO] makeFileLinFunc === TargetFilePath OriginFilePath ===
    echo. [INFO] %TargetFilePath%
    echo. [INFO] %OriginFilePath%

    echo. [INFO] makeFileLinFunc === LocalOriginFilePath LocalTargetFilePath ===
    set LocalOriginFilePath=%1
    set LocalTargetFilePath=%2
    echo. [Debug] %LocalTargetFilePath%
    echo. [Debug] %LocalOriginFilePath%
    set OldFileName=%LocalTargetFilePath%
    rem set NewFolderName=%LocalTargetFolderPath%-%date:~3,4%-%date:~8,2%-%date:~11,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%
    set NewFileName=%LocalTargetFilePath%-%date:~3,4%%date:~8,2%%date:~11,2%%time:~0,2%%time:~3,2%%time:~6,2%
    if exist %LocalTargetFilePath% (
        echo. [INFO] 已经存在文件
        echo. [INFO] makeFileLinkFunc === OldFileName ===
        echo. [Debug]%OldFileName%
        echo. [INFO] makeFileLinkFunc === NewFileName ===
        echo. [Debug] %NewFileName%
        echo. [INFO] 重命名文件
        ren %OldFileName% %NewFileName%
    ) else (
        echo. [INFO] 不存在文件
        rem dir
    )
    echo. [INFO] 创建文件软链接 （需要使用超级管理员权限执行）
    mklink /D %LocalTargetFilePath% %LocalOriginFilePath%
    echo.  [INFO] makeFileLinFunc === End ===
goto:eof

:: 函数: 设置文件夹链接
:makeFolderLinkFunc    - makeFolderLinkFunc
    echo. [INFO] makeFolderLinkFunc === Start ===
    echo. [INFO] makeFolderLinkFunc === 1 2 ===
    echo. [Debug] %1
    echo. [Debug] %2

    echo. [INFO] makeFolderLinkFunc === TargetFolderPath OriginFolderPath ===
    echo. [Debug] %TargetFolderPath%
    echo. [Debug] %OriginFolderPath%

    echo. [INFO] makeFolderLinkFunc === LocalOriginFolderPath LocalTargetFolderPath ===
    set LocalOriginFolderPath=%1
    set LocalTargetFolderPath=%2
    echo. [Debug] %LocalTargetFolderPath%
    echo. [Debug] %LocalOriginFolderPath%
    set OldFolderName=%LocalTargetFolderPath%
    rem set NewFolderName=%LocalTargetFolderPath%-%date:~3,4%-%date:~8,2%-%date:~11,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%
    set NewFolderName=%LocalTargetFolderPath%-%date:~3,4%%date:~8,2%%date:~11,2%%time:~0,2%%time:~3,2%%time:~6,2%
    if exist %LocalTargetFolderPath% (
        echo. [INFO] 已经存在文件夹
        echo. [INFO] makeFolderLinkFunc === OldFolderName ===
        echo. [Debug]%OldFolderName%
        echo. [INFO] makeFolderLinkFunc === NewFolderName ===
        echo. [Debug] %NewFolderName%
        echo. [INFO] 重命名文件夹
        mv %OldFolderName% %NewFolderName%
    ) else (
        echo. [INFO] 不存在文件夹
        rem dir
    )
    echo. [INFO] 创建文件夹软链接 （需要使用超级管理员权限执行）
    mklink /D %LocalTargetFolderPath% %LocalOriginFolderPath%

    echo. [INFO] makeFolderLinkFunc === End ===
goto:eof

:: 函数: 永久环境变量-用户变量
:setUserEnvValue    - setUserEnvValue
    echo. [INFO] setUserEnvValue === Start ===
    echo. [INFO] 永久环境变量-用户变量
    echo. [INFO] setUserEnvValue === LocalUserName ===
    set LocalUserName=%1
    echo. %LocalUserName%

    echo. [INFO] setUserEnvValue === LocalUserValue ===
    set LocalUserValue=%2
    echo. %LocalUserValue%

    echo. [Debug] 查看旧变量path
    echo. %PATH%
    rem echo. [INFO] 删除
    rem setx  %LocalUserName% ""
    echo. [INFO] 添加/修改
    setx %LocalUserName% %LocalUserValue%

    echo. [INFO] setUserEnvValue === End ===
goto:eof

:: 函数: 永久环境变量-用户变量Path
:setUserEnvPathValue    - setUserEnvPathValue
    echo. [INFO] setUserEnvPathValue === Start ===
    echo. [INFO] 永久环境变量-用户变量Path
    echo. [INFO] setUserEnvValue === LocalUserPathValue ===
    set LocalUserPathValue=%1
    echo. [INFO] setUserEnvValue === LocalUserValue ===
    echo. %LocalUserPathValue%
    echo. [INFO] === is path 
    echo. [INFO] 获取变量
    echo. [Debug] 查看旧变量path
    echo. %PATH%
    echo. [INFO] 修改变量path
    setx "path" "%path%;%%%LocalUserPathValue%%%;"
    echo. [Debug] 查看更改后的变量path
    echo. %PATH%
    echo. [INFO] setUserEnvPathValue === End ===
goto:eof

:: 函数: 永久环境变量-系统环境变量
:setSystemEnvValue    - setSystemEnvValue
    echo. [INFO] setSystemEnvValue === Start ===
    echo. [INFO] 永久环境变量-系统环境变量 
    echo. [Debug] %1
    echo. [Debug] %2
    echo. [INFO] setSystemEnvValue === LocalSystemName ===
    set LocalSystemName=%1
    echo. %LocalSystemName%
    echo. [INFO] setSystemEnvValue === LocalSystemValue ===
    set LocalSystemValue=%2
    echo. %LocalSystemValue%

    echo. [INFO] 获取变量
    echo. [Debug] 查看旧变量
    wmic ENVIRONMENT where "name='%LocalSystemName%'" get UserName,VariableValue
    echo. [INFO] 添加/修改变量
    echo "删除原变量"
    wmic ENVIRONMENT where "name='%LocalSystemName%'" delete
    echo "添加变量"
    wmic ENVIRONMENT create name="%LocalSystemName%",username="<system>",VariableValue="%LocalSystemValue%"

    echo. [Debug] 查看变量
    wmic ENVIRONMENT where "name='%LocalSystemName%'" get UserName,VariableValue
    echo. [INFO] setSystemEnvValue === End ===
goto:eof

:: 函数: 永久环境变量-系统环境变量Path
:setSystemEnvPathValue    - setSystemEnvPathValue
    echo. [INFO] setSystemEnvPathValue === Start ===
    echo. [INFO] 永久环境变量-系统环境变量 
    echo. [Debug] %1
    echo. [Debug] %2
    echo. [INFO] 获取变量
    echo. [Debug] 查看变量
    echo. %PATH%
    wmic ENVIRONMENT where "name='path'" get UserName,VariableValue

    echo. [Debug] 查看变量
    echo. %PATH%
    echo. [INFO] setSystemEnvPathValue === End ===
goto:eof

:: 流程控制函数
:processControlMain
    :: 打印当前系统时间
    echo. Start Runing...
    echo. %date:~0,18%  %time:~0,-3% 
    rem echo. %date:~3,4% 
    rem echo. %date:~8,2% 
    rem echo. %date:~11,2% 
    rem echo. %time:~0,10% 
    rem echo. %time:~0,2% 
    rem echo. %time:~3,2% 
    rem echo. %time:~6,2% 
    :: 此处是你的代码
    :: ...
    echo. [INFO] processControlMain === ThisShellPath ===
    set ThisShellPath=%str%
    echo. [INFO] %ThisShellPath%
    set OriginFolderPath=%ThisShellPath%
    rem set TargetFolderPath=C:\%HOMEPATH%\AppData\Local\lf
    set TargetFolderPath=%HOMEDRIVE%%HOMEPATH%\AppData\Local\lf 
    echo. [INFO] %OriginFolderPath%
    echo. [INFO] %TargetFolderPath%
    call:makeFolderLinkFunc %OriginFolderPath% %TargetFolderPath%

    call:setUserEnvValue "XIELF"  %TargetFolderPath%
    call:setUserEnvPathValue "XIELF"

    :: 打印当前系统时间
    echo  %date:~0,18%  %time:~0,-3% 
    echo. The End.
goto:eof

::--------------------------------------------------------
::-- 函数部分结束
::--------------------------------------------------------
```

```
::判断上一条命令执行结果的代码,"成功"用0表示 . 失败"用1表示.
@echo off
net user
IF %ERRORLEVEL% == 0 echo net user 执行成功了!
pause

:: 参考资料
:: - [Windows中的硬链接和软链接（hard link 和 Symbolic link）](https://www.cnblogs.com/Naylor/p/7597869.html)

